<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class City
 * Desc  城市控制器
 * Create on 2025/5/10 13:43
 * Create by wangyafang
 */

namespace app\api\controller\wddj;


use app\common\controller\Api;
use fast\Pinyin;

class City extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\wddj\user\operation\City();
    }
    
    /**
     * Desc 选择省,直辖市
     * Create on 2025/5/10 13:44
     * Create by wangyafang
     */
    public function select_province()
    {
        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }

        $province_list = $this->model
            ->where('status', 'normal')
            ->field("DISTINCT SUBSTRING_INDEX(city, '/', 1) AS province")
            ->select();


        $data = array();
        foreach ($province_list as $v) {
            $data[] = $v['province'];
        }

        $this->success('请求成功',$data);
    }

    /**
     * Desc 选择市
     * Create on 2025/5/10 15:00
     * Create by wangyafang
     */
    public function select_city()
    {
        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }
        $province = $this->request->get('province');
        if (empty($province)) {
            $this->error('参数错误');
        }
        $city_list = $this->model
            ->where('city','like','%'.$province.'%')
            ->where('status', 'normal')
            ->field("
                DISTINCT 
                SUBSTRING_INDEX(SUBSTRING_INDEX(city, '/', 2), '/', -1) AS city
            ")
            ->select();
       foreach ($city_list as &$v) {
           if ($this->model->where('city', 'like', '%'.$province.'/'.$v['city'].'/%')->find()) {
               $v->child_status = 1;
           } else {
               $v->child_status = 2;
           }

       }
        $this->success('请求成功',$city_list);
    }

    /**
     * Desc 选择区县
     * Create on 2025/5/10 15:14
     * Create by wangyafang
     */
    public function select_area()
    {
        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }
        $city = $this->request->get('city');
        if (empty($city)) {
            $this->error('参数错误');
        }

        $area_list = $this->model
            ->where('status', 'normal')
            ->where('city', 'like', '%'.$city.'/%')
            ->field("
                DISTINCT 
                SUBSTRING_INDEX(city, '/', -1) AS area
            ")
            ->select();
        $data = array();
        foreach ($area_list as $v) {
            $data[] = $v['area'];
        }

        $this->success('请求成功',$data);
    }
}



 